// ++++++++++++++++++++++++++++++++++++++++
// 《Go 语言+Vue.js 实战派——基于 Gin 框架》源码
// ++++++++++++++++++++++++++++++++++++++++
// 作者公众号：源码大数据
// 自媒体账号（抖音、视频号、快手、B站、知乎）：廖显东-ShirDon
// 仓库地址：https://gitee.com/shirdonl/goGinVue.git
// 仓库地址：https://github.com/shirdonl/goGinVue.git
// 博客：https://www.shirdon.com
// 购买图书：https: //item.jd.com/14958064.html
// ++++++++++++++++++++++++++++++++++++++++

package main

import (
	"github.com/gin-gonic/gin"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"net/http"
)

// Customer用户信息
type Customer struct {
	Id    int `gorm:"primaryKey"`
	Name  string
	Phone string
}

func main() {
	dsn := "root:a123456@tcp(127.0.0.1:3306)/gin_vue_ch6?charset=utf8mb4&parseTime=True&loc=Local" // 替换为开发者的连接字符串
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	//按 ID 查找单个客户
	var customer Customer
	result := db.First(&customer, 1)
	if result.Error != nil {
		panic("failed to find customer")
	}
	customer.Phone = "18888888899"
	result = db.Model(&customer).UpdateColumn("phone", "18888888899")

	if result.Error != nil {
		panic("failed to update customer")
	}
}

func updateUser(db *gorm.DB, c *gin.Context) {
	var customer Customer

	if err := c.BindJSON(&customer); err != nil {
		// 处理错误
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	result := db.Model(&customer).Updates(Customer{Name: customer.Name, Phone: customer.Phone})

	if result.Error != nil {
		// 处理错误
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Customer updated successfully"})
}
